<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Content_Model extends CI_Model
{

	function __construct(){
		$this->db = $this->load->database('default', TRUE); 
	}

	function get_featured_product(){

		$this->db = $this->load->database('default', TRUE); 
		
		$this->db->limit(12);
		$this->db->where('status',1);
		$this->db->order_by('product_id DESC');

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();		

	}

	function get_random_product($limit=1){
		$this->db->limit($limit);
		$this->db->where('status',1);
		$this->db->order_by('rand()');

		$query = $this->db->get('product');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();	
	}
	
	function get_category(){
		$this->db->where('parent', 0);
		$this->db->order_by('sort_order ASC');
		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return NULL;
	}

	function get_category_child($parent){
		$this->db->where('parent', $parent);
		$this->db->order_by('sort_order ASC');
		$query = $this->db->get('product_category');
		if ($query->num_rows() > 0) return $query->result();
		return array();
	}
	
	function get_slide(){
		$query = $this->db->query('select * from slide where status = "1" limit 4');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}

	//add by dena
	function get_country(){		
		$this->db->order_by('name ASC');
		$this->db->where('status', 1);
		$query = $this->db->get('country');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}

	function get_country_by_id($country_id){
		$this->db->where('country_id', $country_id);
		$this->db->order_by('name ASC');
		$query = $this->db->get('country');
		if ($query->num_rows() > 0 ) return $query->row();
		return NULL;
	}

	function get_country_by_name($name){
		$this->db->where('name', $name);
		$query = $this->db->get('country');
		if ($query->num_rows() > 0 ) return $query->row();
		return NULL;
	}

	function get_region_by_id($zone_id){
		$this->db->where('zone_id', $zone_id);
		$query = $this->db->get('zone');
		if ($query->num_rows() > 0 ) return $query->row();
		return NULL;
	}

	function get_region_by_country_id($country_id){
		$this->db->where('country_id', $country_id);
		$this->db->where('status', 1);
		$this->db->order_by('name ASC');
		$query = $this->db->get('zone');
		if ($query->num_rows() > 0 ) return $query->result();
		return array();
	}
	
	function getCurrency(){
		$query = $this->db->query('select * from currency');
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	/*/*function get_customer_by_id($customer_id){
		$this->db->select_max('customer_id');
		$this->db->where('customer_id', $customer_id);
		$query = $this->db->get('customer');
		
		//echo $this->db->last_query();
		
		if ($query->num_rows() > 0 ) return $query->row();
		return NULL();
	}*/
	
	function get_store_by_product($product_id){
		$query = $this->db->query('select * from store where toko_id in (select toko_id from product where product_id = "'.$product_id.'")');
		
		if($query -> num_rows() > 0){
			foreach ($query -> result() as $rows){
				$data[] = $rows;	
			}	
			return $data;
		}	
	}
	
	function get_store($user_id){
		$this->db->where('user_id', $user_id);

		$query = $this->db->get('store');
		if ($query->num_rows() == 1) return $query->row();
		return NULL;

	}
	
}